package tests;

import static org.junit.Assert.*;

import org.junit.Test;

import coreSimulation.Passenger;
import coreSimulation.Baggage;

/*
 * Test the methods, constructor and exceptions of the passenger class
 */
public class PassengerTest {

	Passenger p;

	// test getName() method
	@Test
	public void testGetName() {
		p = new Passenger("Bob Dobbs", "SA-12345678");
		assertEquals("The getName() method failed for the Passenger class",
				"Bob Dobbs", p.getName());
	}

	// test getBookingRef() method
	@Test
	public void testGetBookingRef() {
		p = new Passenger("Bob Dobbs", "SA-12345678");
		assertEquals(
				"The getBookingRef() method failed for the Passenger class",
				"SA-12345678", p.getBookingRef());
	}

	/*
	 * This method tests both getBaggage and addBaggage methods of the Passenger
	 * class
	 */
	@Test
	public void testAddGetBaggage() {
		p = new Passenger("Bob Dobbs", "SA-12345678");
		Baggage bag1 = new Baggage(10, 10, 10, 10);
		p.addBaggage(bag1);

		Baggage bag2 = p.getBaggage();
		// check that the bag added and returned from the Passenger are equal
		assertEquals("getBaggage method failed", bag1, bag2);
	}

	/*
	 * test the IllegalStateException when instantiating a passenger with an
	 * empty name
	 */
	@Test(expected = IllegalStateException.class)
	public void testPassengerNoName() {
		p = new Passenger("", "SA-12345678");

	}

	/*
	 * test IllegalStateException for incorrectly formatted booking ref numeric
	 * part of booking ref too short
	 */
	@Test(expected = IllegalStateException.class)
	public void testPassengerInvalidRef1() {
		p = new Passenger("John Smith", "SA-1234");

	}

	/*
	 * test IllegalStateException for incorrectly formatted booking ref part of
	 * booking ref too long
	 */
	@Test(expected = IllegalStateException.class)
	public void testPassengerInvalidRef2() {
		p = new Passenger("John Smith", "SA-123456789");
	}

	/*
	 * test IllegalStateException for incorrectly formatted booking ref
	 * incorrect number format
	 */
	@Test(expected = IllegalStateException.class)
	public void testPassengerInvalidRef3() {
		p = new Passenger("John Smith", "SA-X23456789");
	}

	/*
	 * test IllegalStateException for incorrectly formatted booking ref
	 * incorrectly formatted prefix - should be 'SA-'
	 */
	@Test(expected = IllegalStateException.class)
	public void testPassengerInvalidRef4() {
		p = new Passenger("John Smith", "XX-12345678");

	}

	/*
	 * test the compareTo method, it should return true when comparing two
	 * passengers with the same booking ref
	 */
	@Test
	public void testCompareTo() {
		// Create two passengers with the same booking ref
		Passenger p1 = new Passenger("Erica Eller", "SA-55705175");
		Passenger p2 = new Passenger("Dennis Dillow", "SA-55705175");

		// check comparison of the two passengers
		assertTrue(p1.compareTo(p2) == 0);
	}

	/*
	 * test the equals method, it should return true for two passengers with the
	 * same booking ref
	 */
	@Test
	public void testEquals() {
		// Two passengers with the same booking ref
		Passenger p1 = new Passenger("Erica Eller", "SA-55705175");
		Passenger p2 = new Passenger("Dennis Dillow", "SA-55705175");

		assertTrue(p1.equals(p2));
	}

	/*
	 * test the hashCode method, two passengers with the same booking ref
	 * should have equivalent hash codes
	 */
	@Test
	public void testHashCode() {
		// Two passengers with the same booking ref
		Passenger p1 = new Passenger("Erica Eller", "SA-55705175");
		Passenger p2 = new Passenger("Dennis Dillow", "SA-55705175");

		assertEquals(p1.hashCode(), p2.hashCode());
	}

	/*
	 * test adding and getting excess charges
	 */
	@Test
	public void testExcessCharges() {
		Passenger p = new Passenger("John Smith", "SA-12345678");
		p.addExcessCharge(99.99);
		assertEquals(p.getExcessCharges(), 99.99, 0);
	}

}
